<?php
#============================
#	baoxiu 报修管理
#	权限控制: 
#   (1) 报修管理
#   (2) 报修审核
#============================
class baoxiuControlPage extends commonControlPage 
{
	/*[(1)报修管理]*/ 
	//opt = add edit view delete
	public function baoxiumanageModule()
	{
		$opt = in_array($_GET['opt'],array('add','edit','view','dec')) ? $_GET['opt'] : 'view';
		switch($opt)
		 {
			 case 'view' :  //列表
				$this->view->view_act = ' class="active" '; 
        		call_user_func(array($this,"viewAction"));
			 break;
			 case 'add' :   //添加
			 	$this->view->add_act = ' class="active" '; 
				if (POST && AJAX) 
				{
					$next_uid = $_POST['next_uid'];
					$data = $this->baoxiuValidate();
					$data['uid']      = $this->user['uid'];
					$data['username'] = $this->user['username'];
					$data['audit_state'] = '1';  //未审批
					$data['created_time'] = $_ENV['time'];  
					$data['co_member_id'] = $next_uid;
					$customer = $_ENV['db']->fetch("SELECT * FROM `customer` WHERE `customer_id` = '" . $_POST['customer_id'] . "'");
					$data['customer_id'] = $customer['customer_id'];
					$data['customer_name'] = $customer['customer_name'];
					InsertTable('baoxiu',$data);
					$this->outputJson(1,'操作成功');
				}
				$customers = $_ENV['db']->fetchAll("SELECT * FROM customer WHERE  state='1'");
				$this->view->customers = BindComboxs("customer_id","customer_id",$customers,'customer_id','customer_name');
				$node_id = $_ENV['config']['baoxiu_audit_nodes'][1];
				$this->view->next_user_list = class_permission::GetUsersByNodeID($node_id);  // 获取下级操作员列表
			 break;
			 case 'edit' :  //编辑
				$this->view->edit_act = ' class="active" '; 
				if (POST && AJAX) 
				{
					$next_uid = $_POST['next_uid'];
					$data = $this->baoxiuValidate();
					$data['co_member_id'] = $next_uid;
					UpdateTable('baoxiu',$data,array('bid'=>$_POST['bid']));
					$this->outputJson(1,'操作成功');
				}
				$bid = $_GET['bid'];
				$baoxiu =  $_ENV['db']->fetch("SELECT * FROM `baoxiu` WHERE `bid` = '{$bid}'");
				$node_id = $_ENV['config']['baoxiu_audit_nodes'][1];
				$customers = $_ENV['db']->fetchAll("SELECT * FROM customer WHERE  state='1'");
				$this->view->customers = BindComboxs("customer_id","customer_id",$customers,'customer_id','customer_name',$baoxiu['customer_id'] );
				$this->view->next_user_list = class_permission::GetUsersByNodeID($node_id);  // 获取下级操作员列表
				$this->view->baoxiu = $baoxiu;
			 break;
			 case 'dec' : //详情
			 	$this->view->dec_act = ' class="active" '; 
			 	$bid = $_GET['bid'];
				$baoxiu =  $_ENV['db']->fetch("SELECT * FROM `baoxiu` WHERE `bid` = '{$bid}'");
				$this->view->state = $this->GetOrderStatus($baoxiu['audit_state']);
				$this->view->baoxiu = $baoxiu;
			 break;
			 case  'delete':  //删除
			 break;
		 }
		 $this->view->opt = $opt;
		 $this->showMainPage('admincp/baoxiu/baoxiu_manage');
	}
	
	/*报修信息列表*/
    public function viewAction($state='') 
	{
		$page= (int)$_GET['page_no'];
        $where = ' 1 ';
        if (!empty($_GET['customname'])) 
		{
            $keyword = urldecode($_GET['customname']);
            $where .= " and customer_name like '{$keyword}%'";
        }
		if($state || $state=='0')
		{
			$where .= " and audit_state='{$state}'";
		}
		$this->view->lists = $this->PrePage('baoxiu','bid',$where,$page,'bid desc','app=baoxiu&mod=manager&opt=list&page_no=tpt');
    }
	
	//获得单据状态
	private function GetOrderStatus($code)
	{
		switch($code)
		{
			case '1':  return '<span style="color:blue; font-size:14px;">未审批</span>';  break;
			case '2':  return '<span style="color:green; font-size:14px;">批准</span>';  break;
		}
		return '未审批';
	}
	
	/*数据验证
	 1添加验证 2更新验证*/
    private function baoxiuValidate() 
	{
        $data = getRequests(array('title','content','customer_id'));    
        $length = mb_strlen($data['title'],'UTF-8');
        if ($length < 1 || $length > 20) 
		{
           $this->outputJson(0,'客户名称长度应该为1到20位字符','title');
        }
	    if(!$data['content'])
		{
			$this->outputJson(0,'内容不能为空!','content');
		}
		if(!$data['customer_id'])
		{
			$this->outputJson(0,'客户不能为空!','customer_id');
		}
		if(!$_POST['next_uid'])
		{
			$this->outputJson(0,'下级操作人不能为空!','next_uid');
		}
        return $data;
    }
	
	
	/*[(1)报修管理]*/ 
	//opt = view post
	public function auditModule()
	{
		$opt = in_array($_GET['opt'],array('view','post')) ? $_GET['opt'] : 'view';
		switch($opt)
		 {
			 case 'view' :  //列表
				$this->view->view_act = ' class="active" '; 
        		$this->viewAction('1');
			 break;
			 case 'post' :   //确定审核
			 	$this->view->post_act = ' class="active" '; 
				$node_id = $_ENV['config']['baoxiu_audit_nodes'][1];
				$baoxiu = $_ENV['db']->fetch("SELECT * FROM `baoxiu` WHERE `bid` = '{$_GET['bid']}'");
				if (!$baoxiu) $this->showMessage('指定的对象不存在',AJAX?0:'back');
				if (POST && AJAX) 
				{
					$next_uid = (int)$baoxiu['co_member_id'];              //下级审核人
					$param = array();
					$param['audit_state']     = 2;                          //审核通过
					$param['audit1_uid']      = $this->user['uid'];         //审核人ID
					$param['audit1_username'] = $this->user['username'];    //审核人姓名
					$param['audit1_dateline'] = $_ENV['time'];              //审核时间
					$param['mark'] = $_POST['mark'];                        //信息反馈
					if ($next_uid) 
					{   # 如果是审核1，需要验证下级操作员
						if (!class_permission::ValidateNodeByUser($node_id,$next_uid)) $this->outputJson(0,'指定的下级操作员没有足够的权限');
					}
					UpdateTable('baoxiu',$param,array('bid'=>$_GET['bid']));
					$this->outputJson(1,'审核成功');
				}
				
				$this->view->state = $this->GetOrderStatus($baoxiu['audit_state']);
				$this->view->baoxiu = $baoxiu;
			 break;
		 }
		 $this->view->opt = $opt;
		 $this->showMainPage('admincp/baoxiu/baoxiu_audit');
	}
}